/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.management.user.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.tompai.common.constants.ReturnStatusConstant;
import com.tompai.common.enums.OperateStatusEnums;
import com.tompai.common.model.SysUser;
import com.tompai.common.model.SysUserRoleRelation;
import com.tompai.common.util.EnvUtils;
import com.tompai.management.user.service.SysUserRoleRelationService;
import com.tompai.management.user.service.SysUserService;
import com.tompai.management.user.web.vo.SysUserInfoQueryVo;
import com.tompai.management.user.web.vo.SysUserInfoVo;
import com.tompai.management.user.web.vo.SysUserVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户管理界面
 * 
 * @author liinux@qq.com
 * @date 2019/10/9
 */
@Slf4j
@Controller
@Api(tags = "用户信息管理{用户管理:/user/manager}")
@RequestMapping("/user")
public class UserController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysUserRoleRelationService sysUserRoleRelationService;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@ApiOperation(value = "获取用户列表")
	@GetMapping
	public String sysUserInfo() {
		return "user/sys-user-info";
	}

	@ApiOperation(value = "查询指定用户信息")
	@GetMapping("/get-sys-user-info/{id}")
	@ResponseBody
	@ApiImplicitParam(name = "id", value = "主键id", type = "path")
	public SysUser getSysUserInfo(@PathVariable("id") String id) {
		return sysUserService.getSysUserInfo(id);
	}

	@ApiOperation(value = "查询所有用户角色信息")
	@GetMapping("/select-sys-user-info")
	@ResponseBody
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "当前页数", defaultValue = "1", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "limit", value = "每页数量", defaultValue = "10", dataType = "int", paramType = "query", example = "1,10") })
	public SysUserInfoVo selectSysUserInfo(SysUser sysUser, int page, int limit) {

		PageInfo<SysUserVo> pageInfo = sysUserService.selectSysUserInfo(sysUser, page, limit);
		SysUserInfoVo sysUserInfoVo = new SysUserInfoVo();
		sysUserInfoVo.setCode(OperateStatusEnums.SUCCESS.getStringValue());
		sysUserInfoVo.setCount(Long.valueOf(pageInfo.getTotal()).intValue());
		sysUserInfoVo.setData(pageInfo.getList());
		return sysUserInfoVo;
	}

	@ApiOperation(value = "查询用户关联角色信息")
	@GetMapping("/select-sys-user-relation-role-info")
	@ResponseBody
	public List<SysUserRoleRelation> selectSysUserRelationRoleInfo(String userId) {
		return sysUserRoleRelationService.selectSysUserRelationRoleInfo(userId);
	}

	@ApiOperation(value = "添加/编辑用户用户信息")
	@PostMapping("/add-or-update-sys-user-info")
	@ResponseBody
	public ReturnStatusConstant addOrUpdateSysUserInfo(@RequestBody SysUserInfoQueryVo sysUserInfoQueryVo) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {

			if (StringUtils.hasText(sysUserInfoQueryVo.getId())) {
				SysUser sysUserInfo = sysUserService.getSysUserInfo(sysUserInfoQueryVo.getId());
				if (sysUserInfo != null && !sysUserInfo.getPassword().equals(sysUserInfoQueryVo.getPassword())) {
					// 密码加密
					sysUserInfoQueryVo.setPassword(passwordEncoder.encode(sysUserInfoQueryVo.getPassword()));
				}
				sysUserInfoQueryVo.beforeUpdate();
				sysUserInfoQueryVo.setUpdateBy(EnvUtils.getCurrentUserId());
				sysUserService.update(sysUserInfoQueryVo);
			} else {
				// 密码加密
				sysUserInfoQueryVo.setPassword(passwordEncoder.encode(sysUserInfoQueryVo.getPassword()));
				sysUserInfoQueryVo.beforeInsert();
				sysUserInfoQueryVo.setCreateBy(EnvUtils.getCurrentUserId());
				sysUserService.insert(sysUserInfoQueryVo);
			}

			/** 更新角色信息 1.删除原有的角色关联 2.添加新的角色关联 */
			sysUserRoleRelationService.updateUserRoleRelation(sysUserInfoQueryVo);

			returnCode.setCode(ReturnStatusConstant.SUCCESS);//
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			log.error("添加/编辑用户用户信息：", e);
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}

	@ApiOperation(value = "删除用户用户信息")
	@PostMapping("/delete-sys-user-info")
	@ResponseBody
	public ReturnStatusConstant deleteSysUserInfo(
			@RequestParam("id") @ApiParam(name = "id", value = "主键id") String id) {
		ReturnStatusConstant returnCode = new ReturnStatusConstant();
		try {
			sysUserService.deleteByPrimaryKey(id);//
			sysUserRoleRelationService.deleteByUserId(id);
			returnCode.setCode(ReturnStatusConstant.SUCCESS);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_MESSAGE);
		} catch (Exception e) {
			log.error("删除信息出错：", e);
			returnCode.setCode(ReturnStatusConstant.FAIL);
			returnCode.setMessage(ReturnStatusConstant.SUCCESS_FAIL);
		}
		return returnCode;
	}
}
